//
//  ViewController.swift
//  对象池模式（图书管理系统）
//
//  Created by 崔博 on 2017/7/24.
//  Copyright © 2017年 崔博. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
}

class Book {
    
    let author: String
    let title: String
    let stockNumer: Int
    var reader: String?
    var checkoutCount = 0
    init(author: String, title: String, stock: Int) {
        self.author = author
        self.title = title
        self.stockNumer = stock
    }
}

class Pool<T> {
    private var data = [T]()
    init(items: [T]) {
        data.reserveCapacity(data.count)
        for item in items {
            data.append(item)
        }
    }
    
    func getFromPool() -> T? {
        var result: T?
        if (data.count > 0) {
            result = self.data[0]
            self.data.remove(at: 0)
        }
        return result
    }
    func returnToPool(item: T) {
        self.data.append(item)
    }
}
